#!/bin/bash
#用于系统初始化 base redhat7 sles12


BasePath=$(cd `dirname $0`; pwd)

#查看系统版本
 echo
 echo "检查系统版本..."
 echo
 echo "系统版本为："
 echo -e "\e[0;31m      $(cat /etc/os-release|grep -w PRETTY_NAME|awk -F\" '{print $2}')    \e[0m"
 echo

#cpu、内存检查
 echo
 echo "cpu,内存统计:"
 echo  -e "\e[0;31m     ↓\e[0m"
 echo "逻辑CPU核数："
 echo -e "\e[0;31m$(grep 'processor' /proc/cpuinfo| wc -l) core\e[0m"
 echo "内存："
 echo -e "\e[0;31m$(awk '/MemTotal/{printf("%.1f\n",$2/1024/1024)}' /proc/meminfo)G\e[0m"
 echo "================================================================================"

#关闭防火墙
  echo
  echo "防火墙状态检查..."
  echo
  for i in {1..3}
  do

      if [[ "$(systemctl status firewalld |grep Active)" =~ "dead" ]];
       then
          echo -e "\e[0;31m 防火墙已是关闭状态\e[0m"
          break
       elif [[ "$(systemctl status firewalld |grep Active)" =~ "running" ]];
         then
           systemctl stop firewalld
           systemctl disable firewalld
         else
           echo "防火墙状态异常，请检查"
      fi

  done


#关闭SELINUX
  echo
  echo "SELINUX状态检查..."
  echo
  for i in {1..3}
  do
      case $(getenforce) in
      Disabled)
          if [ "$(awk -F"=" '/^SELINUX=/{print $2}' /etc/selinux/config)" = "disabled" ];
             then
               echo -e "\e[0;31m Selinux已关闭\e[0m"
               break
             else
               sed -i 's/^SELINUX=.*/SELINUX=disabled/g' /etc/selinux/config
          fi
          ;;
      Permissive)
          if [ "$(awk -F"=" '/^SELINUX=/{print $2}' /etc/selinux/config)" = "disabled" ];
             then
               echo -e "\e[0;31m Selinux已是关闭状态\e[0m"
               break
             else
               sed -i 's/^SELINUX=.*/SELINUX=disabled/g' /etc/selinux/config
          fi
          ;;
      Enforcing)
          setenforce 0
          if [ "$(awk -F"=" '/^SELINUX=/{print $2}' /etc/selinux/config)" = "disabled" ];
             then
               break
             else
               sed -i 's/^SELINUX=.*/SELINUX=disabled/g' /etc/selinux/config
          fi
          ;;
      esac
  done
  setenforce 0;
  echo "================================================================================"

#修改openfile
 echo
 echo "openfile状态检查..."
 echo

 if [ "$(ulimit -n)" -lt "65535" ];
   then
   if [[ "$(grep -v '^#' /etc/security/limits.conf)" =~ "* soft nofile 65536" ]];
      then
        echo
      else
        echo -e "* soft nofile 65536\n* hard nofile 65536" >>/etc/security/limits.conf
   fi
   egrep -v "^#|^$" /etc/security/limits.conf
   ulimit -HSn 65536
 fi

 echo "openfile数值为：$(ulimit -n)"
 echo "================================================================================"



 echo "hostname: $(hostname)"
 echo "ip: $(hostname -i)"
 echo "================================================================================"

#修改系统参数
#net.ipv4.tcp_timestamps = 0
#net.ipv4.tcp_tw_recycle = 0
#net.ipv4.tcp_tw_reuse = 0
 echo "开始修改系统参数"
 sleep 1s
 echo "#稠银企e家系统优化"                           > /etc/sysctl.d/ehome.conf;
 echo "net.ipv4.ip_forward          = 1   "          >> /etc/sysctl.d/ehome.conf;
 echo "net.ipv4.tcp_timestamps      = 0   "          >> /etc/sysctl.d/ehome.conf;
 echo "net.ipv4.tcp_fin_timeout     = 2   "          >> /etc/sysctl.d/ehome.conf;
 echo "net.ipv4.tcp_tw_recycle      = 0   "          >> /etc/sysctl.d/ehome.conf;
 echo "net.ipv4.tcp_tw_reuse        = 0   "          >> /etc/sysctl.d/ehome.conf;
 echo "net.ipv4.tcp_syncookies      = 1   "          >> /etc/sysctl.d/ehome.conf;
 echo "net.ipv4.ip_local_port_range = 32768 60999"   >> /etc/sysctl.d/ehome.conf;
 echo "net.ipv4.tcp_max_syn_backlog = 1024"          >> /etc/sysctl.d/ehome.conf;
 echo "net.core.somaxconn           = 1024"          >> /etc/sysctl.d/ehome.conf;
 echo "net.ipv4.tcp_max_tw_buckets  = 5000"          >> /etc/sysctl.d/ehome.conf;
 echo "net.ipv4.tcp_syn_retries     = 1   "          >> /etc/sysctl.d/ehome.conf;
 echo "net.ipv4.tcp_synack_retries  = 1   "          >> /etc/sysctl.d/ehome.conf;
 echo "net.core.netdev_max_backlog  = 1000"          >> /etc/sysctl.d/ehome.conf;
 echo "net.ipv4.tcp_max_orphans     = 2000"          >> /etc/sysctl.d/ehome.conf;
 echo " ">>/etc/sysctl.d/ehome.conf;
 sysctl -p;
 echo -e "\e[0;31m系统参数优化完毕\e[0m"
 sysctl -a |egrep "timestamps|tcp_tw";